package com.clothes.recommender
import org.apache.spark.sql.{DataFrame, SparkSession}
import org.apache.spark.sql.functions.desc

object hot {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder().appName("HotRecommendation").master("local[*]").getOrCreate()
    import spark.implicits._

    // 读取数据
    val productsDF = spark.read
      .format("jdbc")
      .option("url", "jdbc:mysql:///clothes_recommender_system?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC")
      .option("dbtable", "clothes")
      .option("user", "root")
      .option("password", "123456")
      .load()

    // 根据评论数降序排列，选取前200条数据
    val hotProductsDF: DataFrame = productsDF
      .orderBy(desc("comment_count"))
      .limit(200)

    hotProductsDF.write
      .format("jdbc")
      .option("url", "jdbc:mysql:///clothes_recommender_system?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC")
      .option("dbtable", "hotRecommend")
      .option("user", "root")
      .option("password", "123456")
      .mode("overwrite")
      .save()

  }
}
